package com

import android.app.Application
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory

class App : Application(),ViewModelStoreOwner {

    companion object{

        const val APP_ID = "wx02b8fb6141fc4681"
        const val APP_SECRET = "5eb4fa7522df8cc5235e659f05ede285"

        lateinit var wxApi: IWXAPI
        lateinit var app:App

        fun toast(str:String){
            Toast.makeText(app,str,Toast.LENGTH_SHORT).show()
        }
    }

    lateinit var viewStore:ViewModelStore
    lateinit var factory:ViewModelProvider.Factory

    override fun onCreate() {
        super.onCreate()
        app = this;

        viewStore = ViewModelStore()
        factory = ViewModelProvider.AndroidViewModelFactory.getInstance(this)

        wxApi = WXAPIFactory.createWXAPI(this, APP_ID,false)
        wxApi.registerApp(APP_ID)
    }

    override fun getViewModelStore(): ViewModelStore {
        return viewStore
    }

    fun <T:ViewModel> getGlobalModel(modelClazz:Class<T>):T{
        return ViewModelProvider(this,factory).get(modelClazz)
    }
}